package com.samsung.spen.sshare.networking.sendreceivefile;

import java.util.ArrayList;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.networking.networkservice.SendReceiveService;
import com.samsung.spen.sshare.networking.networkservice.SendingDataTask;

import android.R.integer;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class FileAdapter extends ArrayAdapter<SubFileExtend> {

	private static final String TAG = "FILEADAPTER";
	private Context mContext;
	private int mLayoutRsc;
	private int mType;
	private ArrayList<SubFileExtend> mFiles = new ArrayList<SubFileExtend>();	
	private static String mIP;
	private static String mPort;

	public FileAdapter(Context context, int layoutResourceId,
			ArrayList<SubFileExtend> objects, int type) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mLayoutRsc = layoutResourceId;
		this.mFiles = objects;
		this.mType = type;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View row = convertView;
		FileHolder holder = null;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(mLayoutRsc, parent, false);
			holder = new FileHolder();
			holder.txtFileName = (TextView) row.findViewById(R.id.txtFileName);
			holder.txtFileSize = (TextView) row.findViewById(R.id.txtFileSize);
			holder.btnAccept = (Button) row.findViewById(R.id.btnAccept);
			holder.pgPercent=(ProgressBar)row.findViewById(R.id.pgPercent);
			if (mType == SendReceiveActivity.REQUEST) {

				holder.btnAccept.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i(TAG, "" + position);
//						SendReceiveActivity.mBoundService.send("INDEX%"+position,null);
						mFiles.get(position).setIndex(position);
						new SendingDataTask().execute(mIP,mPort,SendReceiveService.INDEX,""+position);
					}
				});
			}else{
				holder.btnAccept.setVisibility(Button.GONE);
			}

			row.setTag(holder);
		}

		holder = (FileHolder) row.getTag();
		SubFileExtend file = mFiles.get(position);
		
		holder.txtFileName.setText(file.getFileName());
		holder.txtFileSize.setText(file.getFileSize());
		holder.pgPercent.setProgress(file.percent);
		return row;
	}
	
	public void setIP(String ip){
		this.mIP=ip;
	}
	
	public void setPort(String port) {
		this.mPort=port;
	}

	public class FileHolder {
		TextView txtFileName;
		TextView txtFileSize;
		Button btnAccept;
		ProgressBar pgPercent;
	}

}
